<template>
<div class="friend-content">
  <ul class="friend">
    <li class="friend-item add-friend-item" @click="addFriend">
      <span class="iconfont icon-icon_roundadd"></span>
      添加好友
    </li>
    <li class="friend-item" @click="itemClick">
        <div class="avatar">
          <img src="/static/images/library.jpg" alt="头像">
        </div>
        <div class="friendinfo">
            <span class="nickname">测试用Item</span>
        </div>
        <div class="clearfix"></div>
    </li>
    <li class="friend-item" @click="itemClick($event)" v-for="(item, index) in $store.getters.getFriendList" :key="index" :data-id="index">
        <div class="avatar">
          <img :src="item.avatar" alt="头像">
        </div>
        <div class="friendinfo">
          <span class="nickname">{{item.nickname}}</span>
        </div>
        <div class="clearfix"></div>
    </li>
  </ul>
</div>
</template>

<script>
export default {
  name: 'friend',
  data () {
    return {
      selectedId: ''
    }
  },
  methods: {
    itemClick: function (event) {
      // console.log(event.currentTarget.dataset.id)
      this.selectedId = event.currentTarget.dataset.id
      this.$store.commit('setCurrentChattingId', this.selectedId)
      this.$store.commit('setRightPanelType', 'singlechat')
    },
    addFriend: function () {
      let id = prompt('请输入好友ID')
      if (id === null) return
      // if (id.toString().length !== 32) { // 或者用正则/[0-9]{32}/
      if (!/[0-9]{8}/.test(id)) { // 预设数据添加好友
        alert('输入的ID不符合规定')
        return
      }
      confirm('好友申请已发送')
    }
  },
  watch: {
  },
  components: {
  },
  created: function () {
    this.$store.commit('setLeftPanelType', 'friend')
  }
}
</script>

<style lang="scss">
.friend-content{
  width: 100%;
  height: 100%;
}
.add-friend-item{
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  span{
    font-size: 150%;
    margin: 15px;
  }
}
.friend{
  background: whitesmoke;
  height: 100%;
  overflow-y: scroll;
}
.friend-item{
  height: 45px;
  color: black;
  background: white;
  border-bottom: 1px solid whitesmoke;
  overflow: hidden;   /*解除外边距合并*/
  .avatar{
    float: left;
    margin: 5px;
    img{
      width: 35px;
      height: 35px;
      border-radius: 5px;
    }
  }
  .friendinfo{
    box-sizing: border-box;
    margin: 5px;
    .nickname{
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      display: block;
      line-height: 35px;
    }
  }
  &:hover{
    cursor: pointer;
  }
}
.selected{
  background: #eee;
}
</style>
